#include<stdio.h>  
#include<stdlib.h>  
#include<netinet/in.h>  
#include<sys/socket.h>  
#include<arpa/inet.h>  
#include<string.h>  
#include<unistd.h>  
#include <pthread.h>

#include "./include/base-define.h"
#include "./include/extern.h"
#include "./include/mongo-client.h"
#include "./include/frame.h"
#include "./include/proto-arp.h"
#include "./include/proto-ip.h"

#include "./include/proto-tcp.h"
#include "./include/proto-ssh.h"
#include "./include/proto-udp.h"
#include "./include/proto-bootp.h"
#include "./include/proto-104apci.h"


#include "./include/redis-client.h"
#include "./include/getvalue.h"
#include "./include/proto-vlan.h"
#include "./include/device.h"
#include "./include/base64.h"
#include "./include/station_dev.h"
#include "./include/alarm.h"
#include "./include/proto-wlan.h"

#include "./include/manager/mredis.h"
#include "./include/manager/mdevice.h"

redisContext * sman_redis_cli,*sman_redis_cli_res;

void initSmanRediscli(char *ipaddr,int port)
{
    sman_redis_cli = connectRedisSer(ipaddr,port);
    if(sman_redis_cli == NULL)
    {
        printf("connect mananger redis %s fail\n",ipaddr);
        exit(1);
    }
    return;
}

void closeSmanRediscli()
{
    closeRedisSer(sman_redis_cli);
    return;
}

void runDevBaseConf()//方法
{	//req=>object
    char *baseConfJson="{\"man_type\":\"0\",\"device_id\":\"37010500198\",\"heartbeat\":\"25\",\"status\":\"1\",\"alarm_freqs\":\"25\",\"write_freqs\":\"25\",\"write_counts\":\"65535\",\"hpalarmopen\":\"1\",\"hpalarm_freqs\":\"25\",\"staalarmopen\":\"0\",\"staalarm_freqs\":\"255\",\"commwrite_freqs\":\"25\",\"commwrite_count\":\"65535\",\"wlan_alarm_type\":\"0\",\"wlan_alarm_result\":\"0\",\"hotpoint_dev\":\"devname\",\"wlancap_dev\":\"devname\",\"commcap_dev\":\"devname\",\"man_ipaddr\":\"127.0.0.1\",\"man_port\":\"6379\",\"dev_ipaddr\":\"127.0.0.1\",\"dev_port\":\"6379\"}";
	char *base64Buf;
	//转换成base64
    base64Buf = Base64Encode(baseConfJson, strlen(baseConfJson), 0);
    //存进sman_redis_cli
	lpushSmanConf(sman_redis_cli,base64Buf);
    free(base64Buf);
	//等待sman_redis_cli的应答
    char * baseConfResp=blpopSmanConfR(sman_redis_cli);
	//返回的是base64的
    char * respDecode=Base64Decode(baseConfResp, strlen(baseConfResp), 0);
	//
	//"{\"result\":\"SUCCESS\",\"des\":\"Write sdev conf success.\"}";
	//"{\"result\":\"FAIL\",\"des\":\"Write sdev conf redis fail.\"}";
	//
    printf("Recive resp:%s\n",respDecode);

    if(baseConfResp) free(baseConfResp);if(respDecode) free(respDecode);
    return;
}

void runDevProtoConf()
{
    char *baseConfJson="{\"man_type\":\"1\",\"device_id\":\"37010500198\",\"dev_ipaddr\":\"127.0.0.1\",\"dev_port\":\"6379\",\"tcp\":[\"0\",\"1519555808\",\"1519555908\"],\"udp\":[\"1\",\"1519556808\",\"1519556908\"],\"apil\":[\"1\",\"1519556808\",\"1519556908\"],\"boot\":[\"1\",\"1519556808\",\"1519556908\"]}";
    char *base64Buf;

    base64Buf = Base64Encode(baseConfJson, strlen(baseConfJson), 0);
    lpushSmanConf(sman_redis_cli,base64Buf);
    free(base64Buf);

    char * baseConfResp=blpopSmanConfR(sman_redis_cli);
    char * respDecode=Base64Decode(baseConfResp, strlen(baseConfResp), 0);

    printf("Recive resp:%s\n",respDecode);

    if(baseConfResp) free(baseConfResp);if(respDecode) free(respDecode);
    return;
}

void runSdevConf()
{
    char *baseConfJson="{\"man_type\":\"2\",\"device_id\":\"37010500199\",\"ipaddr\":\"514231589\",\"gmac\":\"0c:12:62:bd:21:e0\",\"strategy\":\"0\",\"dev_ipaddr\":\"127.0.0.1\",\"dev_port\":\"6379\",\"tcp\":[\"0\"],\"udp\":[\"1\"],\"boot\":[\"0\"],\"arp\":[\"1\"],\"ssh\":[\"1\"],\"ssh4\":[\"1\"],\"ssh0\":[\"1\"],\"ssh1\":[\"1\"],\"ssh2\":[\"1\"]}";
    char *base64Buf;

    base64Buf = Base64Encode(baseConfJson, strlen(baseConfJson), 0);
    lpushSmanConf(sman_redis_cli,base64Buf);
    free(base64Buf);

    char * baseConfResp=blpopSmanConfR(sman_redis_cli);
    char * respDecode=Base64Decode(baseConfResp, strlen(baseConfResp), 0);

    printf("Recive resp:%s\n",respDecode);

    if(baseConfResp) free(baseConfResp);if(respDecode) free(respDecode);
    return;
}

void runHPdevConf()
{
    char *baseConfJson="{\"man_type\":\"3\",\"device_id\":\"37010500199\",\"dev_ipaddr\":\"127.0.0.1\",\"dev_port\":\"6379\",\"hpdev1\":[\"12:12:12:12:12:12\",\"0\"],\"hpdev2\":[\"13:13:13:13:13:13\",\"1\"],\"hpdev3\":[\"13:13:13:13:13:13\",\"0\"],\"hpdev4\":[\"13:13:13:13:13:13\",\"1\"],\"hpdevx\":[\"1a:13:13:1a:13:1a\",\"1\"],\"hpdev5\":[\"13:13:13:13:13:13\",\"1\"],\"hpdev8\":[\"13:13:13:13:13:13\",\"1\"],\"hpdev9\":[\"13:13:13:13:13:13\",\"1\"],\"hpdev10\":[\"13:13:13:13:13:13\",\"1\"]}";
    char *base64Buf;

    base64Buf = Base64Encode(baseConfJson, strlen(baseConfJson), 0);
    lpushSmanConf(sman_redis_cli,base64Buf);
    free(base64Buf);

    char * baseConfResp=blpopSmanConfR(sman_redis_cli);
    char * respDecode=Base64Decode(baseConfResp, strlen(baseConfResp), 0);

    printf("Recive resp:%s\n",respDecode);

    if(baseConfResp) free(baseConfResp);if(respDecode) free(respDecode);
    return;
}

void runWlanSdevConf()
{
    char *baseConfJson="{\"man_type\":\"4\",\"device_id\":\"37010500199\",\"dev_ipaddr\":\"127.0.0.1\",\"dev_port\":\"6379\",\"hpdev1\":[\"12:12:12:12:12:12\",\"0\"],\"hpdev2\":[\"13:13:13:13:13:13\",\"1\"],\"hpdev3\":[\"13:13:13:13:13:13\",\"0\"],\"hpdev4\":[\"13:13:13:13:13:13\",\"1\"],\"hpdevx\":[\"1a:13:13:1a:13:1a\",\"1\"],\"hpdev5\":[\"13:13:13:13:13:13\",\"1\"],\"hpdev8\":[\"13:13:13:13:13:13\",\"1\"],\"hpdev9\":[\"13:13:13:13:13:13\",\"1\"],\"hpdev10\":[\"13:13:13:13:13:13\",\"1\"]}";
    char *base64Buf;

    base64Buf = Base64Encode(baseConfJson, strlen(baseConfJson), 0);
    lpushSmanConf(sman_redis_cli,base64Buf);
    free(base64Buf);

    char * baseConfResp=blpopSmanConfR(sman_redis_cli);
    char * respDecode=Base64Decode(baseConfResp, strlen(baseConfResp), 0);

    printf("Recive resp:%s\n",respDecode);

    if(baseConfResp) free(baseConfResp);if(respDecode) free(respDecode);
    return;
}

void runSyscmd()
{
    char *baseConfJson="{\"man_type\":\"5\",\"device_id\":\"37010500199\",\"dev_ipaddr\":\"127.0.0.1\",\"dev_port\":\"6379\",\"cmd\":\"echo ljlk; sleep 10; ls -l\"}";
    char *base64Buf;

    base64Buf = Base64Encode(baseConfJson, strlen(baseConfJson), 0);
    lpushSmanConf(sman_redis_cli,base64Buf);
    free(base64Buf);

    char * baseConfResp=blpopSmanConfR(sman_redis_cli);
    char * respDecode=Base64Decode(baseConfResp, strlen(baseConfResp), 0);

    printf("Recive resp:%s\n",respDecode);

    if(baseConfResp) free(baseConfResp);if(respDecode) free(respDecode);
    return;
}

int main()
{
    //应当从配置库中读取管理中心的redis 服务的ip地址和端口

    initSmanRediscli(MANAGER_CONF_MAN_REDIS_IP,MANAGER_CONF_MAN_REDIS_PORT);
    // runDevBaseConf();
    // runDevProtoConf();
    // runSdevConf();
    // runHPdevConf();
    // runWlanSdevConf();
    runSyscmd();
    closeSmanRediscli();

    return 0;
}